package com.qimo.zuoyeoa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qimo.zuoyeoa.dao.MessageMapper;
import com.qimo.zuoyeoa.entity.Message;
import com.qimo.zuoyeoa.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MessageMapper messageMapper;
    @Override
    public Message findByNamePass(String messagename, String password) {
        return messageMapper.selectByNamePass(messagename,password);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean append(Message message) {
        return messageMapper.insert(message) > 0;
    }

    @Override
    public PageInfo<Message> findAll(int pageNum, int pageSize) {

        PageHelper.startPage(pageNum,pageSize);
        final List<Message> messages = messageMapper.selectAll();
        PageInfo<Message> pageInfo = new PageInfo<>(messages);
        return pageInfo;
    }

    @Override
    public PageInfo<Message> findByKeyword(int pageNum, int pageSize, String keyword) {
        PageHelper.startPage(pageNum, pageSize);
        List<Message> messages = messageMapper.selectByKeyword(keyword);
        PageInfo<Message> pageInfo = new PageInfo<>(messages);
        return pageInfo;
    }

    @Override
    public Message findById(int id) {
        return messageMapper.selectById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean modify(Message message) {
        return messageMapper.updateById(message)>0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean remove(int id) {
        return messageMapper.deleteById(id) > 0;
    }

    @Override
    public List<Message> findAll() {
        return messageMapper.selectAll();
    }
}
